package com.sto.stoapp.entity;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcel;
import android.os.Parcelable;

public class CameraEntity implements Parcelable{
	private Bitmap draw;
	private static byte[] byteDraw;

	public Bitmap getDraw() {
		return draw;
	}

	public void setDraw(Bitmap draw) {
		this.draw = draw;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		byteDraw = getBytes(draw);
		dest.writeByteArray(byteDraw);// 其实这里写入是有顺序的，先写的先读
	}

	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR = new Creator() {

		@Override
		public CameraEntity[] newArray(int size) {
			return new CameraEntity[size];
		}

		@Override
		public CameraEntity createFromParcel(Parcel source) {
			CameraEntity di = new CameraEntity();
			source.readByteArray(byteDraw);
			di.setDraw(getBitmap(byteDraw));
			return di;
		}
	};

	private static Bitmap getBitmap(byte[] data) {
		return BitmapFactory.decodeByteArray(data, 0, data.length);
	}

	private byte[] getBytes(Bitmap bitmap) {
		ByteArrayOutputStream baops = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baops);
		return baops.toByteArray();
	}
}
